

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc] 参考资料Spring Cloud 官方官网：链接Eureka 官方文档：Home · Netflix&#x2F;eureka Wiki视频资料： SpringCloud 教程 已完结（IDEA 2022.1最新版） 【编程不良人】2021年最新SpringCloud微服务实战教程 一、服务注册中心 概念服务注册中心就是在整个微服务架构中单独提出一个服务，这个服务无需完成系统的业务功能">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring Cloud 服务注册笔记1 Eureka 框架 作为服务注册中心.md">
<meta property="og:url" content="http://example.com/2022/07/19/Spring%20Cloud%20%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E7%AC%94%E8%AE%B01%20Eureka%20%E6%A1%86%E6%9E%B6%20%E4%BD%9C%E4%B8%BA%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc] 参考资料Spring Cloud 官方官网：链接Eureka 官方文档：Home · Netflix&#x2F;eureka Wiki视频资料： SpringCloud 教程 已完结（IDEA 2022.1最新版） 【编程不良人】2021年最新SpringCloud微服务实战教程 一、服务注册中心 概念服务注册中心就是在整个微服务架构中单独提出一个服务，这个服务无需完成系统的业务功能">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/eaef1420ed594a8b95037d2fcafbbc25.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/571aab8eb7ec4e4e92e647c0736ef838.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/509582cfb10e466fa84f62964f09c2b7.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/3803c8b19d354c4aa67ada782cdbad2a.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/66edc7472e95496583e0354e94da56de.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/29607001b4f946798bd142c4a5535aa3.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c6171a7f645842eaa82606a85c752ddd.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/fd6ad397faef440fa48aedf5b61fa109.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/03c6f7afc8fc41cda010c59e1d1d7f75.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/7af5cf71002a46bca1902cd111667283.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/3e9478d5e34f4643929ffc4013d30927.png">
<meta property="article:published_time" content="2022-07-19T07:54:55.000Z">
<meta property="article:modified_time" content="2022-08-22T15:48:31.928Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="SpringCloud">
<meta property="article:tag" content="Eureka">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/eaef1420ed594a8b95037d2fcafbbc25.png">
  
  
  
  <title>Spring Cloud 服务注册笔记1 Eureka 框架 作为服务注册中心.md - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Spring Cloud 服务注册笔记1 Eureka 框架 作为服务注册中心.md"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-07-19 15:54" pubdate>
          2022年7月19日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          9.5k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          79 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Spring Cloud 服务注册笔记1 Eureka 框架 作为服务注册中心.md</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]</p>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><p>Spring Cloud 官方官网：<a target="_blank" rel="noopener" href="https://spring.io/projects/spring-cloud#learn">链接</a><br>Eureka 官方文档：<a target="_blank" rel="noopener" href="https://github.com/Netflix/eureka/wiki">Home · Netflix&#x2F;eureka Wiki</a><br>视频资料：</p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1AL4y1j7RY?p=6">SpringCloud 教程 已完结（IDEA 2022.1最新版）</a></p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1S5411c7hM?p=10">【编程不良人】2021年最新SpringCloud微服务实战教程</a></p>
<h1 id="一、服务注册中心"><a href="#一、服务注册中心" class="headerlink" title="一、服务注册中心"></a>一、服务注册中心</h1><hr>
<p><strong>概念</strong><br>服务注册中心就是在整个微服务架构中单独提出一个服务，这个服务无需完成系统的业务功能，仅仅用来对整个微服务系统的服务进行注册和发现，以及对服务健康状态的监控和管理。<br><strong>作用</strong></p>
<ol>
<li>对配置的微服务中各服务的信息进行存储，如微服务的名称、IP和端口等</li>
<li>可在进行服务调用时通过服务发现查询可用的微服务列表及网络地址进行服务调用</li>
<li>对所有的微服务进行心跳检测，如发现某个服务实例长时间无法访问（90秒），就会从服务注册表移除该实例</li>
</ol>
<p>常见服务注册中心组件有：</p>
<ul>
<li>eureka(netflix团队开发）</li>
<li>zookeeper（java开发）</li>
<li>consul（go开发）</li>
<li>nacos（java开发）</li>
</ul>
<h1 id="二、Eureka-概述"><a href="#二、Eureka-概述" class="headerlink" title="二、Eureka 概述"></a>二、Eureka 概述</h1><hr>
<p>Eureka 是一种 RESTful（代表性状态传输）服务，主要用于 AWS 云中，用于中间层服务器的发现、负载平衡和故障转移。它在 Netflix 中层基础设施中发挥着关键作用。<br>Eureka 包含两个组件：Eureka Server 和 Eureka Client</p>
<ul>
<li>Eureka Server 提供服务注册服务</li>
</ul>
<p>各个微服务节点通过配置启动后，会在 EurekaServer 中进行注册，这样 Eureka Server 中的服务注册表中将会存储所有可用服务节点的信息，服务节点中的信息可在web页面看到</p>
<ul>
<li>Eureka Client 通过注册中心进行访问</li>
</ul>
<p>通常是 Java 客户端，用于简化 Eruka Server的交互，客户端同时具备一个内置的、默认使用轮询（round-robin）算法的负载均衡器。<br>应用启动后，程序将会向 Eureka Server 发送心跳（默认为30秒）。若 Eureka Server 在多个心跳周期内没接收到某个节点的心跳，Eureka Server 将会从服务注册表中把这个服务节点移除（默认90秒）</p>
<p>注：eureka 2.0 已停止更新，而 Spring Cloud eureka 采用的大部分是 1.10.x的版本，最近一次更新时间在21年的12月。<br><img src="https://img-blog.csdnimg.cn/eaef1420ed594a8b95037d2fcafbbc25.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="三、Eureka-自我保护机制"><a href="#三、Eureka-自我保护机制" class="headerlink" title="三、Eureka 自我保护机制"></a>三、Eureka 自我保护机制</h1><hr>
<p>官方文档：<a target="_blank" rel="noopener" href="https://github.com/Netflix/eureka/wiki/Server-Self-Preservation-Mode">https://github.com/Netflix/eureka/wiki/Server-Self-Preservation-Mode</a><br>服务频繁启动时，Eureka Server Web页面会出现错误提示<br><img src="https://img-blog.csdnimg.cn/571aab8eb7ec4e4e92e647c0736ef838.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>默认情况下，若 Eureka Server 在 一定时间内（90秒）没接收到之前连接的某个微服务实例的心跳，则会移除该实例。当网络出现问题，微服务与 Eureka Server 之间无法通信，而 微服务本身正常运行，此时就不该移除实例，为了解决这类问题，Eureka 提供了自我保护机制</p>
<p>自我保护机制：服务端在运行期间会统计心跳失败比例在 15分钟内是否 低于85%，若低于85%，Eureka 服务端会将这些实例保护起来。我们可以在用 SpringBoot整合时，可通过配置来关闭这种保护机制</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-attr">eureka.server.enbale-self-preservation</span>=<span class="hljs-string">false # 关闭自我保护</span><br><br><span class="hljs-attr">eureka.server.eviction-interval-timer-in-ms</span>=<span class="hljs-string">3000 # 超时3秒自动清除</span><br><br><span class="hljs-attr">eureka.instance.lease-expiration-duration-in-seconds</span>=<span class="hljs-string">60 # 修改 eureka server 接收心跳的最长等待时间，默认为90秒</span><br><br><span class="hljs-attr">eureka.instance.lease-renewal-interval-in-seconds</span>=<span class="hljs-string">20	    # 指定客户端多久向 eureka server发送一次心跳，默认是30秒</span><br></code></pre></td></tr></table></figure>
<h1 id="四、Eureka-集群"><a href="#四、Eureka-集群" class="headerlink" title="四、Eureka 集群"></a>四、Eureka 集群</h1><hr>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/09f3bef21b684a439c6aea77df16c492.png#">https://img-blog.csdnimg.cn/09f3bef21b684a439c6aea77df16c492.png#</a> &#x3D;80%x)</p>
<p>这里由于我们是初学，暂时不深入了解其底层原理，先以实践为主。</p>
<h1 id="五、Eureka-使用案例"><a href="#五、Eureka-使用案例" class="headerlink" title="五、Eureka 使用案例"></a>五、Eureka 使用案例</h1><hr>
<p>Eureka 自动注册并发现微服，对服务状态、信息进行几种管理，这样我们可以通过 Eureka 来查询其他服务的信息。</p>
<p>基于上次案例：<a target="_blank" rel="noopener" href="https://blog.csdn.net/Unirithe/article/details/125827360">Spring Cloud 前导篇 | 传统 SpringBoot 项目的延展 | 图书借阅查询案例 | 拆分为多个服务进行部署与交互</a></p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/4f95b158a46d4b35acbd6d8e51a533b0.png#">https://img-blog.csdnimg.cn/4f95b158a46d4b35acbd6d8e51a533b0.png#</a> &#x3D;80%x)</p>
<h2 id="5-1-创建-Eureka-服务端-服务"><a href="#5-1-创建-Eureka-服务端-服务" class="headerlink" title="5.1 创建 Eureka 服务端 服务"></a>5.1 创建 Eureka 服务端 服务</h2><p>引入 Spring Cloud 前 我们要在 <strong>父项目</strong> 导入 spring cloud，之后的服务都是继承这个父项目的</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.cloud<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-cloud-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>Hoxton.SR12<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-comment">&lt;!-- 维护版本 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependencyManagement</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.cloud<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-cloud-dependencies<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>$&#123;spring.cloud.version&#125;<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">type</span>&gt;</span>pom<span class="hljs-tag">&lt;/<span class="hljs-name">type</span>&gt;</span><br>      <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>import<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>  <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencyManagement</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>接着在 Eureka 服务里单独引入 eureka-server 的 起步依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;UTF-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">project</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span><br><span class="hljs-tag">         <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">parent</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>cloud-demo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>cn.uni<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0-SNAPSHOT<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">parent</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">modelVersion</span>&gt;</span>4.0.0<span class="hljs-tag">&lt;/<span class="hljs-name">modelVersion</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>eruka-server<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">properties</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.source</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.source</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">maven.compiler.target</span>&gt;</span>8<span class="hljs-tag">&lt;/<span class="hljs-name">maven.compiler.target</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">properties</span>&gt;</span><br><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.cloud<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-cloud-starter-netflix-eureka-server<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">project</span>&gt;</span><br></code></pre></td></tr></table></figure>
<p>项目配置：application.yml</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">server:</span><br>  <span class="hljs-attr">port:</span> <span class="hljs-number">8761</span><br><span class="hljs-attr">eureka:</span><br>  <span class="hljs-comment"># 修改客户端配置</span><br>  <span class="hljs-attr">client:</span><br>    <span class="hljs-comment"># 不需要将自身服务器注册到 Eureka</span><br>    <span class="hljs-attr">fetch-registry:</span> <span class="hljs-literal">false</span><br>    <span class="hljs-attr">register-with-eureka:</span> <span class="hljs-literal">false</span><br>    <span class="hljs-comment"># 设置 eureka 服务端 URL</span><br>    <span class="hljs-attr">service-url:</span><br>      <span class="hljs-attr">defaultZone:</span> <span class="hljs-string">http://localhost:8761/eureka</span><br></code></pre></td></tr></table></figure>
<p>启动类：EurekaApplication.java<br>（SpringBoot启动除了之前的单例模式以外还支持建造者模式，使用Builder来启动）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> cn.uni;<br><br><span class="hljs-keyword">import</span> org.springframework.boot.WebApplicationType;<br><span class="hljs-keyword">import</span> org.springframework.boot.autoconfigure.SpringBootApplication;<br><span class="hljs-keyword">import</span> org.springframework.boot.builder.SpringApplicationBuilder;<br><span class="hljs-keyword">import</span> org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;<br><br><span class="hljs-meta">@SpringBootApplication</span><br><span class="hljs-meta">@EnableEurekaServer</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">EurekaApplication</span> &#123;<br>    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> &#123;<br>        <span class="hljs-keyword">new</span> <span class="hljs-title class_">SpringApplicationBuilder</span>(EurekaApplication.class)<br>                .web(WebApplicationType.SERVLET)<br>                .run(args);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>启动 Eureka 服务后，访问web端：<code>http://localhost:8761</code></p>
<p><img src="https://img-blog.csdnimg.cn/509582cfb10e466fa84f62964f09c2b7.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>至此，简介的 Eureka 服务端就部署完毕了</p>
<h2 id="5-2-添加-Eureka-客户端"><a href="#5-2-添加-Eureka-客户端" class="headerlink" title="5.2 添加 Eureka 客户端"></a>5.2 添加 Eureka 客户端</h2><p>接下来我们根据将之前的三个web服务添加 Eureka-client，让它们作为客户端来连接当前的服务端<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/e472d41dba95481a9135ec5377b33443.png#">https://img-blog.csdnimg.cn/e472d41dba95481a9135ec5377b33443.png#</a> &#x3D;50%x)</p>
<p>在 book-service、eruka-service 和 user-service 三个模块中同时添加 eruka-client 依赖：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework.cloud<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-cloud-starter-netflix-eureka-client<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>添加依赖后，和之前 eureka 服务端一样，我们要在这三个子服务中配置连接的 eureka 服务端地址，配置为相同的 <a target="_blank" rel="noopener" href="http://localhost:8761/eureka">http://localhost:8761/eureka</a>，即同一个 eureka 服务端<br>application.yml</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">eureka:</span><br>  <span class="hljs-attr">client:</span><br>    <span class="hljs-attr">service-url:</span> <br>      <span class="hljs-attr">defaultZone:</span> <span class="hljs-string">http://localhost:8761/eureka</span><br></code></pre></td></tr></table></figure>

<p>在添加配置后，需在 SpringBoot 启动类 添加 <code>@EnableEurekaClient</code></p>
<h2 id="5-3-启动测试"><a href="#5-3-启动测试" class="headerlink" title="5.3 启动测试"></a>5.3 启动测试</h2><p>注，首先要启动 eureka 服务，否则其他服务连接不到，我们一共启动四个服务<br><img src="https://img-blog.csdnimg.cn/3803c8b19d354c4aa67ada782cdbad2a.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>启动后 访问 Eureka 服务端的 web页面 <a target="_blank" rel="noopener" href="http://localhost:8761/">http://localhost:8761</a><br><img src="https://img-blog.csdnimg.cn/66edc7472e95496583e0354e94da56de.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>可以看到，我们三个服务都连接到了 Eureka 的服务端，但是有个问题，就是这些服务主机名都是默认的 UNKNOWN ，其实我们可以指定每个服务的 SpringBoot项目名称，如：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">spring:</span><br>  <span class="hljs-attr">application:</span><br>    <span class="hljs-string">name:userservice</span><br></code></pre></td></tr></table></figure>
<p>配置服务名称后，再次访问 Eureka 服务端的web页面：<br><img src="https://img-blog.csdnimg.cn/29607001b4f946798bd142c4a5535aa3.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>当服务启动后，eureka 客户端每隔一段时间会向 eureka 服务端发送一次心跳包，这样 eureka 就能知晓我们的服务是否处于正常状态。</p>
<h1 id="六、Eureka-应用"><a href="#六、Eureka-应用" class="headerlink" title="六、Eureka 应用"></a>六、Eureka 应用</h1><hr>
<h2 id="6-1-访问服务"><a href="#6-1-访问服务" class="headerlink" title="6.1 访问服务"></a>6.1 访问服务</h2><p>之前我们在 borrow-servce 模块中实现不同服务之间获取数据，通过 RestTemplate 对象实现，如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> template.getForObject(<span class="hljs-string">&quot;http://localhost:8081/user/&quot;</span> + uid, User.class);<br></code></pre></td></tr></table></figure>
<p>现在 Eureka 框架能完美解决这个问题，我们将请求的地址改为 eureka 的微服务地址（SpringBoot项目名称）即可</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-type">User</span> <span class="hljs-variable">user</span> <span class="hljs-operator">=</span> template.getForObject(<span class="hljs-string">&quot;http://userservice/user/&quot;</span> + uid, User.class);<br></code></pre></td></tr></table></figure>

<h2 id="6-2-负载均衡"><a href="#6-2-负载均衡" class="headerlink" title="6.2 负载均衡"></a>6.2 负载均衡</h2><p>接下来同一个简单的测试来体验这种负载均衡的机制，以 user-service 模块为例，<br>先修改在 controller 里添加一行输出语句，这样就知道当前服务的 controller被调用了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RestController</span><br><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">UserController</span> &#123;<br>    <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>    <span class="hljs-meta">@Resource</span><br>    UserService service;<br><br>    <span class="hljs-meta">@GetMapping(&quot;/user/&#123;uid&#125;&quot;)</span><br>    <span class="hljs-keyword">public</span> User <span class="hljs-title function_">findUserById</span><span class="hljs-params">(<span class="hljs-meta">@PathVariable(&quot;uid&quot;)</span> <span class="hljs-type">int</span> uid)</span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;/user/&#123;uid&#125;/ 被访问了:&quot;</span> + (++count) + <span class="hljs-string">&quot;次&quot;</span>);<br>        <span class="hljs-keyword">return</span> service.getUserById(uid);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>然后，去掉当前服务 application.yml 里的端口配置，之后我们将用一个项目开启不同端口的两个进程。</p>
<p>编辑服务启动的配置：<br><img src="https://img-blog.csdnimg.cn/c6171a7f645842eaa82606a85c752ddd.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>创建文件夹来存放两个服务实例<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/78efb245ae1d4670924b80042b78ee84.png#">https://img-blog.csdnimg.cn/78efb245ae1d4670924b80042b78ee84.png#</a> &#x3D;60%x)</p>
<p>指定服务端口号<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/4ba8446516fb482ba427753fe869dfc6.png#">https://img-blog.csdnimg.cn/4ba8446516fb482ba427753fe869dfc6.png#</a> &#x3D;60%x)</p>
<p>添加新的服务实例<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/71a25aa3137b4c469f951134af1bf342.png#">https://img-blog.csdnimg.cn/71a25aa3137b4c469f951134af1bf342.png#</a> &#x3D;60%x)</p>
<p>然后启动项目，如下图所示：<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/7b8b520727134ac2a74ff3b4f53858d8.png#">https://img-blog.csdnimg.cn/7b8b520727134ac2a74ff3b4f53858d8.png#</a> &#x3D;60%x)</p>
<p>首先检查 Eureka 服务端的 web 页面是否能检测到这四个服务<br><img src="https://img-blog.csdnimg.cn/fd6ad397faef440fa48aedf5b61fa109.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>可以看到，用户服务目前有两个端口号，接下来我们访问 borrow 书籍借阅服务的接口，因为这个接口会调用 userservice的服务，我们可以观察它调用的是两个中哪一个服务。<br><a target="_blank" rel="noopener" href="http://localhost:8083/borrow/1%EF%BC%8C%E6%AF%94%E5%A6%82%E8%AE%BF%E9%97%AE20%E6%AC%A1%EF%BC%8C%E7%BB%93%E6%9E%9C%E6%98%BE%E7%A4%BA%E5%88%B0%E4%B8%A4%E4%B8%AA%E8%BF%9B%E7%A8%8B%E9%83%BD%E6%9C%89%E8%A2%AB%E8%AE%BF%E9%97%AE%E7%9A%84%E8%AE%B0%E5%BD%95%EF%BC%8C%E8%BF%99%E4%BE%BF%E6%98%AF">http://localhost:8083/borrow/1，比如访问20次，结果显示到两个进程都有被访问的记录，这便是</a> Eureka自带的负载均衡机制。<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/6c80705aad254f1a946f6ff6bd145aa5.png#">https://img-blog.csdnimg.cn/6c80705aad254f1a946f6ff6bd145aa5.png#</a> &#x3D;80%x)</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/20b9eb0ab513454db05f735095568a20.png#">https://img-blog.csdnimg.cn/20b9eb0ab513454db05f735095568a20.png#</a> &#x3D;80%x)</p>
<h1 id="七、Eureka-集群应用案例"><a href="#七、Eureka-集群应用案例" class="headerlink" title="七、Eureka 集群应用案例"></a>七、Eureka 集群应用案例</h1><hr>
<p>在之前的实践中，我们的三个服务都通过 Eureka 服务端来访问其他服务的接口，但如果只有一个服务端，往往会存在安全隐患，接下来我们通过搭建一个 Eureka 集群来提升系统的安全性<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/15eceaf3969f4144b1b50f8e12a2c71a.png#">https://img-blog.csdnimg.cn/15eceaf3969f4144b1b50f8e12a2c71a.png#</a> &#x3D;80%x)</p>
<p>设置 Eureka 服务端不同的配置文件，使用同一个项目部署不同的服务<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/14330cfed867439c924baad99ef5b9bf.png#">https://img-blog.csdnimg.cn/14330cfed867439c924baad99ef5b9bf.png#</a>  &#x3D;60%x)</p>
<p>在添加新的配置前，先去掉之前 application.yml 里对端口号的指定，否则后面在创建多个实例时会出现端口冲突</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">server:</span><br>  <span class="hljs-attr">port:</span> <span class="hljs-number">8761</span><br></code></pre></td></tr></table></figure>

<p><code>application-01.yml</code></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">server:</span><br>  <span class="hljs-attr">port:</span> <span class="hljs-number">8761</span><br><span class="hljs-attr">spring:</span><br>  <span class="hljs-attr">application:</span><br>    <span class="hljs-attr">name:</span> <span class="hljs-string">eurekaserver</span><br><span class="hljs-attr">eureka:</span><br>  <span class="hljs-attr">instance:</span><br>     <span class="hljs-comment"># 由于目前是本机部署, 所以需要自定义主机名称，用于区分</span><br>     <span class="hljs-attr">hostname:</span> <span class="hljs-string">eureka01</span><br>  <span class="hljs-comment"># 修改客户端配置</span><br>  <span class="hljs-attr">client:</span><br>    <span class="hljs-comment"># 不需要将自身服务器注册到 Eureka</span><br>    <span class="hljs-attr">fetch-registry:</span> <span class="hljs-literal">false</span><br>    <span class="hljs-comment"># register-with-eureka: true 	默认为 ture，支持将自身服务器注册到其他的 Eureka 服务器，从而支持互相调用</span><br>    <span class="hljs-comment"># 设置 eureka 服务端 URL</span><br>    <span class="hljs-attr">service-url:</span><br>      <span class="hljs-comment"># 写除了自身的其他 Eureka 服务器地址</span><br>      <span class="hljs-attr">defaultZone:</span> <span class="hljs-string">http://eureka02:8762/eureka,</span> <span class="hljs-string">http://eureka03:8763/eureka</span><br></code></pre></td></tr></table></figure>
<p><code>application-02.yml</code></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">server:</span><br>  <span class="hljs-attr">port:</span> <span class="hljs-number">8762</span><br><span class="hljs-attr">spring:</span><br>  <span class="hljs-attr">application:</span><br>    <span class="hljs-attr">name:</span> <span class="hljs-string">eurekaserver</span><br><span class="hljs-attr">eureka:</span><br>  <span class="hljs-attr">instance:</span><br>     <span class="hljs-attr">hostname:</span> <span class="hljs-string">eureka02</span><br>  <span class="hljs-attr">client:</span><br>    <span class="hljs-attr">fetch-registry:</span> <span class="hljs-literal">false</span><br>    <span class="hljs-attr">service-url:</span><br>      <span class="hljs-attr">defaultZone:</span> <span class="hljs-string">http://eureka01:8761/eureka,</span> <span class="hljs-string">http://eureka03:8763/eureka</span><br></code></pre></td></tr></table></figure>
<p><code>application-03.yml</code></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">server:</span><br>  <span class="hljs-attr">port:</span> <span class="hljs-number">8763</span><br><span class="hljs-attr">spring:</span><br>  <span class="hljs-attr">application:</span><br>    <span class="hljs-attr">name:</span> <span class="hljs-string">eurekaserver</span><br><span class="hljs-attr">eureka:</span><br>  <span class="hljs-attr">instance:</span><br>     <span class="hljs-attr">hostname:</span> <span class="hljs-string">eureka03</span><br>  <span class="hljs-attr">client:</span><br>    <span class="hljs-attr">fetch-registry:</span> <span class="hljs-literal">false</span><br>    <span class="hljs-attr">service-url:</span><br>      <span class="hljs-attr">defaultZone:</span> <span class="hljs-string">http://eureka01:8761/eureka,</span> <span class="hljs-string">http://eureka02:8762/eureka</span><br></code></pre></td></tr></table></figure>
<h2 id="7-1-配置本地域名映射"><a href="#7-1-配置本地域名映射" class="headerlink" title="7.1 配置本地域名映射"></a>7.1 配置本地域名映射</h2><p>将之前配置的 eureka实例名都配置指向地址为 127.0.0.1 的映射<br>windows 系统IP地址映射文件的目录在：C:\Windows\System32\drivers\etc\HOSTS，在文件末尾添加：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span> <span class="hljs-string">eureka01</span><br><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span> <span class="hljs-string">eureka02</span><br><span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span> <span class="hljs-string">eureka03</span><br></code></pre></td></tr></table></figure>
<p>添加后保存并完毕文件即可</p>
<h2 id="7-2-设置客户端连接-Eureka-集群"><a href="#7-2-设置客户端连接-Eureka-集群" class="headerlink" title="7.2 设置客户端连接 Eureka 集群"></a>7.2 设置客户端连接 Eureka 集群</h2><p>在之前 eureka 客户端的配置文件里添加内容：<br><code>application.yml</code></p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-attr">eureka:</span><br>  <span class="hljs-attr">client:</span><br>    <span class="hljs-attr">service-url:</span><br>      <span class="hljs-attr">defaultZone:</span> <br>        <span class="hljs-string">http://localhost:8761/eureka,</span> <span class="hljs-string">http://localhost:8762/eureka,</span> <span class="hljs-string">http://localhost:8763/eureka</span><br></code></pre></td></tr></table></figure>

<h2 id="7-3-启动测试"><a href="#7-3-启动测试" class="headerlink" title="7.3 启动测试"></a>7.3 启动测试</h2><p>启动前先创建三个实例，模拟 eureka 集群</p>
<p><img src="https://img-blog.csdnimg.cn/03c6f7afc8fc41cda010c59e1d1d7f75.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>启动顺序是先启动 eureka 服务端，然后再启动它的客户端服务<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/773043d56c4246738a7bd02f6f081882.png#">https://img-blog.csdnimg.cn/773043d56c4246738a7bd02f6f081882.png#</a> &#x3D;50%x)</p>
<p>启动后，访问其中一个 Eureka 服务端的 web 页面，如：<a target="_blank" rel="noopener" href="http://localhost:8761/">http://localhost:8761</a><br><img src="https://img-blog.csdnimg.cn/7af5cf71002a46bca1902cd111667283.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>可以看到，此时的 eureka 集群已经在正常运作了，接下来测试端口：<br><a target="_blank" rel="noopener" href="http://localhost:8083/borrow/1">http://localhost:8083/borrow/1</a><br><img src="https://img-blog.csdnimg.cn/3e9478d5e34f4643929ffc4013d30927.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>可以正常访问到数据，接下来我们随机将一个 eureka 服务端关闭，比如 关闭 eureka03<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/08a33835a4a546d1a3eee4486229a619.png#">https://img-blog.csdnimg.cn/08a33835a4a546d1a3eee4486229a619.png#</a> &#x3D;50%x)</p>
<p>继续访问 <a target="_blank" rel="noopener" href="http://localhost:8083/borrow/1">http://localhost:8083/borrow/1</a>， 同样是有数据返回的。<br>然后继续关闭 eureka02，同样有数据。<br>最后我们将 eureka01 也关闭，看看是否还有数据返回<br>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/2079f78caf884d038686e714c227c939.png#">https://img-blog.csdnimg.cn/2079f78caf884d038686e714c227c939.png#</a> &#x3D;50%x)</p>
<p>此时依然是有结果的，但是会报错：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs yaml"><span class="hljs-string">.DiscoveryClient</span>    <span class="hljs-string">:</span> <span class="hljs-string">DiscoveryClient_BOOKSERVICE/unirithe.mshome.net:bookservice:8082</span> <span class="hljs-bullet">-</span> <span class="hljs-string">was</span> <span class="hljs-string">unable</span> <span class="hljs-string">to</span> <span class="hljs-string">send</span> <span class="hljs-string">heartbeat!</span><br></code></pre></td></tr></table></figure>
<p>然后我们重新启动 eureka 服务端集群，再次访问 web 页面，会发现一切功能正常运作</p>
<p>![在这里插入图片描述](<a target="_blank" rel="noopener" href="https://img-blog.csdnimg.cn/6d08ec8fb90e4356ac91bce65cb8cec9.png#">https://img-blog.csdnimg.cn/6d08ec8fb90e4356ac91bce65cb8cec9.png#</a> &#x3D;60%x)</p>
<p>这就是 eureka 框架的强大所在，至此，eureka 集群创建与测试完毕。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%BE%AE%E6%9C%8D%E5%8A%A1/" class="category-chain-item">微服务</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/SpringCloud/">#SpringCloud</a>
      
        <a href="/tags/Eureka/">#Eureka</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Spring Cloud 服务注册笔记1 Eureka 框架 作为服务注册中心.md</div>
      <div>http://example.com/2022/07/19/Spring Cloud 服务注册笔记1 Eureka 框架 作为服务注册中心/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年7月19日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/07/19/Spring%20Cloud%20%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E7%AC%94%E8%AE%B02%20ZooKeeper%20%E4%BD%9C%E4%B8%BA%E6%9C%8D%E5%8A%A1%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83/" title="Spring Cloud 服务注册笔记2 ZooKeeper 作为服务注册中心">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Spring Cloud 服务注册笔记2 ZooKeeper 作为服务注册中心</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/07/18/Spring%20Cloud%20%E5%89%8D%E5%AF%BC%E7%AF%87%20_%20%E4%BC%A0%E7%BB%9F%20SpringBoot%20%E9%A1%B9%E7%9B%AE%E7%9A%84%E5%BB%B6%E5%B1%95%20_%20%E5%9B%BE%E4%B9%A6%E5%80%9F%E9%98%85%E6%9F%A5%E8%AF%A2%E6%A1%88%E4%BE%8B%20_%20%E6%8B%86%E5%88%86%E4%B8%BA%E5%A4%9A%E4%B8%AA%E6%9C%8D%E5%8A%A1%E8%BF%9B%E8%A1%8C%E9%83%A8%E7%BD%B2%E4%B8%8E%E4%BA%A4%E4%BA%92/" title="Spring Cloud 前导篇 _ 传统 SpringBoot 项目的延展 _ 图书借阅查询案例 _ 拆分为多个服务进行部署与交互">
                        <span class="hidden-mobile">Spring Cloud 前导篇 _ 传统 SpringBoot 项目的延展 _ 图书借阅查询案例 _ 拆分为多个服务进行部署与交互</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
